package com.cetc.sdp.mzga.service.pojo;

import java.util.List;
import java.util.Map;

import org.jooq.DSLContext;
import org.jooq.SelectSelectStep;
import org.jooq.impl.DSL;

import com.cetc.sdp.mzga.service.conn.PhoenixContext;
import com.cetc.sdp.mzga.service.inter.Callable;

public class HbaseRangeScan {

	public static List<String> rangeDistinctScan(String rkeyprofoxstart,String rkeyprofoxend,final String rkeyname,final String getfldname,final String tblName) throws Exception{
		
		StringBuilder conditionB = new StringBuilder();
		conditionB.append(rkeyname).append(">= '").append(rkeyprofoxstart).append("'")
		          .append(" and ")
		          .append(rkeyname).append("< '").append(rkeyprofoxend).append("'");
		final String condition =conditionB.toString();
		
		@SuppressWarnings("unchecked")
		List<String> data = (List<String>) PhoenixContext.execute(new Callable(){

		      @Override
		      public Object call(DSLContext create) {		    	 
		    	  System.out.println(
		    			  create.selectDistinct(DSL.field(getfldname))
		    	          .from(tblName)
		    	          .where(condition)
		    	          .getSQL()
		    	          );
		            List<String> result = (List<String>) create.selectDistinct(DSL.field(getfldname))
		    	          .from(tblName)
		    	          .where(condition)
		    	          .fetch()
		    	          .getValues(0);
 
		            return  result;
		       }		      
		    });		
		return  data;
	}
	
	
	public  static List<Map<String,String>> rangeScan(String rkeyprofoxstart,String rkeyprofoxend,final String rkeyname,final List<String> getfldnames,final String tblName) throws Exception{
		
		StringBuilder conditionB = new StringBuilder();
		conditionB.append(rkeyname).append(">= '").append(rkeyprofoxstart).append("'")
		          .append(" and ")
		          .append(rkeyname).append("< '").append(rkeyprofoxend).append("'");
		final String condition =conditionB.toString();
			
		@SuppressWarnings("unchecked")
		List<Map<String,String>> data = (List<Map<String,String>>) PhoenixContext.execute(new Callable(){

		      @Override
		      public Object call(DSLContext create) {
		    	  
		    	  SelectSelectStep<?> selectstep =null;
		  		if(null==getfldnames || getfldnames.isEmpty()){
		         	 
		  			selectstep = create.select(DSL.field(rkeyname));
		          }
		          else{
		        	  selectstep = create.select(DSL.field(getfldnames.get(0)));
		         	  for(int i = 1; i < getfldnames.size();i++){
		         		 selectstep = selectstep.select(DSL.field(getfldnames.get(i)));
		              }
		          }
		  				
		  		 List<Map<String,Object>>  result = selectstep
		  				 .from(tblName)
		  		         .where(condition)
		  		         .fetch().intoMaps()		  		        
		  		         ;
		    	  System.out.println(selectstep			  				
			  		         .getSQL());
	
		            return  result;
		       }		      
		    });			
		return  data;
	}
	
	
	public static int rangeCount(String rkeyprofoxstart,String rkeyprofoxend,final String rkeyname,final String tblName) throws Exception{
		StringBuilder conditionB = new StringBuilder();
		conditionB.append(rkeyname).append(">= '").append(rkeyprofoxstart).append("'")
		          .append(" and ")
		          .append(rkeyname).append("< '").append(rkeyprofoxend).append("'");
		final String condition =conditionB.toString();
		
		int num = (int)  PhoenixContext.execute(new Callable(){
		    	  
			      @Override
			      public Object call(DSLContext create) {		    	 
			    	  System.out.println(
			    			  create.selectCount()
			    	          .from(tblName)
			    	          .where(condition)
			    	          .getSQL()
			    	          );
			           int result = create.selectCount()
			    	          .from(tblName)
			    	          .where(condition)
			    	          .fetch()
			    	          .get(0)
			    	          .value1()
			    	          ;
	 
			            return  result;
		       }		      
		    });			
		
		
		return num;
	}
}
